package com.songqi.common.security.auth;


import com.alibaba.fastjson2.JSON;
import com.songqi.common.core.constant.HttpStatus;
import com.songqi.common.core.domain.R;
import com.songqi.common.core.utils.I18nMessageUtils;
import com.songqi.common.core.utils.ServletUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;


/**
 * 用户未登录认证失败处理类 返回未授权
 *
 * @author songqi(13776346982)
 */
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
    private static final long serialVersionUID = -8970718410437077606L;


    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        int code = HttpStatus.UNAUTHORIZED;
        String i18nMessage = "认证失败，无法访问系统资源";
        ServletUtils.renderString(response, JSON.toJSONString(R.error(code, i18nMessage)));
    }
}
